package com.leyou.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.exception.LyException;
import com.leyou.trade.dto.OrderVO;
import com.leyou.trade.entity.Order;
import com.leyou.trade.service.OrderService;
import com.leyou.trade.service.PayService;
import com.leyou.trade.utils.PayHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author YYP
 * @date 2021/1/17
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PayHelper payHelper;
    @Autowired
    private OrderService orderService;

    @Override
    public String generatePayUrl(Long id) {
/*        OrderVO order = orderService.queryOrderByOId(id);
        Long actualFee = order.getActualFee();
        System.out.println("============"+actualFee);*/
        //三个参数,订单编号,总价,所购买商品描述
        //查询当前订单对应的订单详情,显示第一个商品即可
        return this.payHelper.getPayUrl(id,/*order.getActualFee()*/1L,"乐友测试");
    }

    /**
     * 微信回调
     * @param paramMap
     */
    @Override
    public void handleNotify(Map<String, String> paramMap) {

        //响应数据校验，1，数据签名，2，验证统一状态，以及业务状态SUCCESS
        this.payHelper.checkResponseSignature(paramMap);
        this.payHelper.checkResultCode(paramMap);
        this.payHelper.checkReturnCode(paramMap);

        String out_trade_no = paramMap.get("out_trade_no");
        String total_fee = paramMap.get("total_fee");

        if (null == out_trade_no || null == total_fee) {
            throw new LyException(400,"支付通知有误,已报警");
        }

        Long orderId = Long.valueOf(out_trade_no);
        Long totalFee = Long.valueOf(total_fee);

        Order order = this.orderService.getById(orderId);

        if (totalFee.longValue() != /*order.getActualFee().longValue()*/1L) {
            throw new LyException(400, "支付通知有误，已报警");
        }

        //TODO 校验通过了，要做的时，改变订单的支付状态
        order.setStatus(2);
        //设置支付时间
        order.setPayTime(new Date());

        //加入乐观锁校验，update tb_order set status = 2 where order_id = #{orderId} and status = #{status}
        this.orderService.update(order,new QueryWrapper<Order>().eq("status",1).eq("order_id",order.getOrderId()));    }
}
